<template>
  <el-container>
    <el-header>
      <el-col span="24">
        <el-input v-model="docUpdateRequest.docTitle" placeholder="请输入智库DOC标题"></el-input>
      </el-col>
    </el-header>
    <el-main style="height: 100%">
      <el-col span="17">
        <editor-box :url="docVo.docUrl"></editor-box>
      </el-col>
      <el-col span="6" style="border: ridge 1px; margin-left: 3px;height: 11vh">
        <doc-aside-box></doc-aside-box>
        <el-button @click="update" type="primary">
          更新
        </el-button>
        <el-button  @click="openHistory=true" type="info">
          历史版本
        </el-button>
      </el-col>
    </el-main>
    <el-dialog title="历史版本" :visible.sync="openHistory" :modal="false" width="50%" center="true">
      <doc-history-box :docId="docVo.id"></doc-history-box>
    </el-dialog>
  </el-container>
</template>

<script>
import docAsideBox from "@/components/docComponents/docAsideBox";
import EditorBox from "@/components/Editor/editorBox";
import docHistoryBox from "@/components/docComponents/docHistoryBox";
import {ttAPI} from "@/api/ttApi";
import {DocUpdateRequest} from "@/entity/doc/DocUpdateRequestBody";
export default {
  name: "docUpdateRequest",
  components: {docHistoryBox, EditorBox, docAsideBox},
  data() {
    return {
      docUpdateRequest: new DocUpdateRequest(),
      docVo:this.$route.params.docVo,
      openHistory:false
    }
  },
  methods:{
    async update() {
      this.docUpdateRequest.labelList = JSON.stringify(this.docUpdateRequest.labelList)
      await this.axios.post(ttAPI.updatedocs, this.docUpdateRequest)
          .then(res => {
            if (!res.data.code) {
              this.$message.success(res.data.message)
            } else {
              this.$message.error(res.data.message)
            }
            this.docUpdateRequest.labelList=JSON.parse(this.docUpdateRequest.labelList)
          })
          .catch(err => {
            this.$message.info(err.data.message)
          })

    },
    getHistory(){

    }
  },
 async mounted() {
   await this.axios.get(ttAPI.getdocByid + this.$route.query.id)
       .then(res => {
         this.docVo = res.data.data
         this.docUpdateRequest.docTitle = this.docVo.docTitle
         this.docUpdateRequest.userdocid = this.docVo.userdocid
         this.docUpdateRequest.id = this.docVo.id
         this.docUpdateRequest.type = this.docVo.typeId
         this.docUpdateRequest.labelList = JSON.parse(this.docVo.labelList)
         this.docUpdateRequest.docIntroduction = this.docVo.docIntroduction
         this.docUpdateRequest.featImg = this.docVo.featImg
         this.docUpdateRequest.schedId = this.docVo.scoped
         this.docUpdateRequest.publishTime  = this.docVo.publishTime
         var xhr = new XMLHttpRequest();
         xhr.open('GET', this.docVo.docUrl,true);
         xhr.send(null);
         xhr.onreadystatechange= ()=>{
           setTimeout(()=>{
             this.resetSetItem('updateText',xhr.responseText)
           },500)
         }
       })
   window.addEventListener('setItem', () => {
     this.docUpdateRequest.docText = sessionStorage.getItem('updateText')
   })
 }
}
</script>

<style scoped>

</style>